﻿/*
    C# ASP.Net PotBS Data Services API Library
    Copyright (C) 2009  blackstaff1995

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Net;
using System.IO;
using System.Xml;

namespace PotbsData
{
    public class XmlClient
    {
        /// <summary>
        /// PotBS Data Service User ID
        /// </summary>
        protected const string id = "273312";

        /// <summary>
        /// PotBS Data Service API Key
        /// </summary>
        protected const string key = "PBSV-JZDP-J3MQ-OAVW";

        /// <summary>
        /// PotBS Assembled Credentials String
        /// </summary>
        protected const string credentials = "userid=" + id + "&apikey=" + key;

        /// <summary>
        /// Gets Status of all FLS Servers
        /// Cache Requirement: 1 min
        /// </summary>
        /// <returns>XML string containing FLS response</returns>
        static public XDocument GetServerStatus()
        {
            string svcLoc = "http://data.burningsea.com/servers";
            return getXmlResponse(svcLoc);

        }

        /// <summary>
        /// Get Status of Requested FLS Server
        /// Cache Requirement: ?
        /// </summary>
        /// <param name="serverName">Name of Server for Request</param>
        /// <returns>XML string containing FLS response</returns>
        static public XDocument GetServerStatus(string serverName)
        {
            string svcLoc = string.Format("http://data.burningsea.com/servers/{0}", serverName);
            return getXmlResponse(svcLoc);
        }

        /// <summary>
        /// Get Status of all Landmarks for Requested Server
        /// Cache Requirement: ?
        /// </summary>
        /// <param name="serverName">Name of Server for Request</param>
        /// <returns>XML string containing FLS response</returns>
        static public XDocument GetLandmarkStatus(string serverName)
        {
            string svcLoc = string.Format("http://data.burningsea.com/servers/{0}/landmarks", serverName);
            return getXmlResponse(svcLoc);
        }

        /// <summary>
        /// Get Status of Requested Landmark on Requested Server.
        /// Cache Requirement: ?
        /// </summary>
        /// <param name="serverName">Name of Server for Request</param>
        /// <param name="landmarkName">Name of Landmark for Request</param>
        /// <returns>XML string containing FLS response</returns>
        static public XDocument GetLandmarkStatus(string serverName, string landmarkName)
        {
            string svcLoc = string.Format("http://data.burningsea.com/servers/{0}/landmarks/{1}", serverName, landmarkName);
            return getXmlResponse(svcLoc);
        }

        /// <summary>
        /// Request Landmark Location on Requested Server
        /// Cache Requirement: ?
        /// </summary>
        /// <param name="serverName">Name of Server for Request</param>
        /// <param name="landmarkName">Name of Landmark for Request</param>
        /// <returns>XML string containing FLS response</returns>
        static public XDocument GetLandmarkLoc(string serverName, string landmarkName)
        {
            string svcLoc = string.Format("http://data.burningsea.com/servers/{0}/landmarks/{1}/location", serverName, landmarkName);
            return getXmlResponse(svcLoc);
        }

        /// <summary>
        /// Request Landmark Location Scaled to viewport X by Y
        /// Cache Requirement: ?
        /// </summary>
        /// <param name="serverName">Name of Server for Request</param>
        /// <param name="landmarkName">Name of Landmark for Request</param>
        /// <param name="x">Max X Coordinate</param>
        /// <param name="y">Max Y Coordinate</param>
        /// <returns>XML string containing FLS response</returns>
        static public XDocument GetLandmarkLoc(string serverName, string landmarkName, string x, string y)
        {
            string svcLoc = string.Format("http://data.burningsea.com/servers/{0}/landmarks/{1}/location/{2}/{3}", serverName, landmarkName, x, y);
            return getXmlResponse(svcLoc);
        }

        /// <summary>
        /// Request Current RVR Status for Server
        /// Cache Requirement: ?
        /// </summary>
        /// <param name="serverName">Name of Server for Request</param>
        /// <returns>XML string containing FLS response</returns>
        static public XDocument GetServerScoreboard(string serverName)
        {
            string svcLoc = string.Format("http://data.burningsea.com/servers/{0}/scoreboard", serverName);
            return getXmlResponse(svcLoc);
        }

        /// <summary>
        /// Requests Static Strings from FLS
        /// Cache Requirement: 24 Hours
        /// </summary>
        /// <returns>XML string containing FLS response</returns>
        static public XDocument GetStatics()
        {
            string svcLoc = "http://data.burningsea.com/static/strings";
            return getXmlResponse(svcLoc);
        }

        /// <summary>
        /// Workhorse Function - Forms HTTP Request, Reads Response, Returns Respone
        /// </summary>
        /// <param name="svcLoc">The location of the requested service</param>
        /// <returns>XML string containing FLS response</returns>
        static private XDocument getXmlResponse(string svcLoc)
        {
            Uri uri = new Uri(svcLoc);

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
            request.Method = WebRequestMethods.Http.Post;
            request.ContentLength = credentials.Length;
            request.ContentType = "application/x-www-form-urlencoded";

            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(credentials);
            }

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                XDocument xml = XDocument.Parse(reader.ReadToEnd());
                return xml;
            }
        }
    }
}
